class ApplicationController < ActionController::Base
    protect_from_forgery
  
  private
  def authenticate_user
    if session[:user_id] && User.find(session[:user_id])
      return true 
    else
      redirect_to(:controller => 'sessions', :action => 'new')
      return false
    end
  end
  
  def save_login_state
    if session[:user_id] && User.find(session[:user_id])
      redirect_to :controller =>:product_backlogs, :action => :show#root_path , :notice => "Logged in!"
      return false
    else
      return true
    end
  end
  
  def current_user
    @current_user ||= User.find(session[:user_id]) if session[:user_id]
    
      if @current_user
      #Falls der Nutzer kein Projekt ausgewählt hat, wähle ein Projekt als ausgewähltes
      if @current_user.current_project_id.nil?
        ptu = ProjectToUser.find_by_user_id(@current_user.id)
        @current_user.current_project_id = ptu.project_id  unless ptu.nil?
      end
      
      return @current_user
    else
      #redirect_to(:controller => 'sessions', :action => 'new')
      return false      
    end
  end
#  def current_user_role 
#    roles = Role.where(:rolename_id => current_user.rolename_id, :company_id=> current_user.company_pid) if current_user
#    @current_user_role ||= roles[0] if roles.count == 1
#  end
  
  
  def isCompanyOwner
    if current_user #&& current_user.rolename_id==4
      return true
    else
      render :partial => "layouts/denied"
      return false
    end
  end  
  def isNotCompanyOwner
    if current_user #&& current_user.rolename_id==4
      render :partial => "layouts/denied"
      return false
    else
      return true
    end
  end 
  # def getLineChartData
    # allUS = UserStory.where(product_backlog_pid: ProductBacklog.find_or_create_by_project_pid(current_user.project_id).id)
    # min = DateTime.current.to_f
    # max = DateTime.current.to_f
    # allUS.each do |us|
      # min = us.created_at.to_f if us.created_at.to_f<min
    # end
    # x=0
    # b = Hash.new
    # while x <= 10
      # a = []
       # a[0]=0
       # a[1]=0
      # allUS.each do |us|
        # if us.created_at.to_f < min+((max-min)*x/10)
          # a[0]+=1
          # a[1]+=1 if (us.user_story_state_pid==6 and us.updated_at.to_f < min+((max-min)*x/10))
        # end
      # end
      # d = DateTime.current
      # d=(min+((max-min)*x/10))
      # # b[min+((max-min)*x)] = a
      # b[Time.at(d).to_formatted_s(:short)] = a
      # x += 1
    # end
#     
    # return b
  # end
  def getLineChartData
    allUS = UserStory.where(product_backlog_pid: ProductBacklog.find_or_create_by_project_pid(current_user.current_project_id).id)
    min = DateTime.current.to_f
    max = DateTime.current.to_f
    allUS.each do |us|
      min = us.created_at.to_f if us.created_at.to_f<min
    end
    x=0
    b = Hash.new
    while x <= 10
      a = []
       a[0]=0
       a[1]=0
      allUS.each do |us|
        history = UserstoryHistory.where(:userstory_id => us.id)
        acc_history = nil
        history.each do |h|
          if h.date.to_f < min+((max-min)*x/10) and (acc_history == nil or acc_history.date.to_f < h.date.to_f)
            acc_history = h
          end
        end
        if acc_history != nil && acc_history.user_story_rating_id != nil
          usr = UserStoryRating.find_by_id(acc_history.user_story_rating_id)
          rating = usr.rating
          a[0]+= rating
          a[1]+= rating if (acc_history.userstorystate_id==5)
        end
      end
      d = DateTime.current
      d=(min+((max-min)*x/10))
      # b[min+((max-min)*x)] = a
      b[Time.at(d).to_formatted_s(:short)] = a
      x += 1
    end
    
    return b
  end
   

  helper_method :getLineChartData
  helper_method :isCompanyOwner
  helper_method :isNotCompanyOwner
  helper_method :current_user
  helper_method :current_user_role
end
